package cn.tedu._egnotice1113.egnotice1113.controller;

import cn.tedu._egnotice1113.egnotice1113.base.response.Result;
import cn.tedu._egnotice1113.egnotice1113.mapper.NoticeMapper;
import cn.tedu._egnotice1113.egnotice1113.pojo.dto.NoticeAddParam;
import cn.tedu._egnotice1113.egnotice1113.pojo.dto.NoticeUpdateParam;
import cn.tedu._egnotice1113.egnotice1113.pojo.entity.Notice;
import cn.tedu._egnotice1113.egnotice1113.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._egnotice1113.egnotice1113.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/13 - 18:50
 */
@Slf4j
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/add")
    public Result addNotice(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("前端传入的数据：" + noticeAddParam.toString());
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(1L);
        notice.setIsDelete(0);
        int i = noticeMapper.insertMapper(notice);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/list")
    public Result getNoticeList(){
        List<NoticeListVO> list = noticeMapper.getNoticeVOList();
        return Result.ok(list);
    }

    @GetMapping("/detail")
    public Result getDetailById(Long id){
        log.debug("前端传入的数据：" + id.toString());
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.getNoticeDetailInfoVOById(id);
        return Result.ok(noticeDetailInfoVO);
    }

    @PostMapping("/del")
    public Result deleteNoticeById(Long id){
        log.debug("前端传入的数据：" + id.toString());
        int i = noticeMapper.deleteNoticeById(id);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }


    @PostMapping("/update")
    public Result updateNoticeById(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("前端传入的数据：" + noticeUpdateParam.toString());
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int i = noticeMapper.updateNoticeById(notice);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }
}
